<?php

if (!function_exists('system_setting')) {
    /**
     * 获取系统设置值
     * 
     * @param string $key 设置键名
     * @param mixed $default 默认值
     * @return mixed
     */
    function system_setting($key, $default = null)
    {
        try {
            // 首先尝试从Laravel配置中获取
            $configValue = config("system.{$key}");
            if ($configValue !== null) {
                return $configValue;
            }
            
            // 如果配置中没有，从数据库获取
            return \App\Models\SystemSetting::get($key, $default);
        } catch (\Exception $e) {
            return $default;
        }
    }
}

if (!function_exists('system_settings')) {
    /**
     * 获取多个系统设置值
     * 
     * @param array $keys 设置键名数组
     * @return array
     */
    function system_settings(array $keys)
    {
        $result = [];
        foreach ($keys as $key) {
            $result[$key] = system_setting($key);
        }
        return $result;
    }
}

if (!function_exists('public_system_settings')) {
    /**
     * 获取所有公开的系统设置
     * 
     * @return array
     */
    function public_system_settings()
    {
        try {
            return \App\Models\SystemSetting::getPublicSettings();
        } catch (\Exception $e) {
            return [];
        }
    }
}

if (!function_exists('set_system_setting')) {
    /**
     * 设置系统配置值
     * 
     * @param string $key 设置键名
     * @param mixed $value 设置值
     * @return bool
     */
    function set_system_setting($key, $value)
    {
        try {
            \App\Models\SystemSetting::set($key, $value);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
}

if (!function_exists('site_name')) {
    /**
     * 获取站点名称
     * 
     * @return string
     */
    function site_name()
    {
        return system_setting('site_name', config('app.name'));
    }
}

if (!function_exists('site_description')) {
    /**
     * 获取站点描述
     * 
     * @return string
     */
    function site_description()
    {
        return system_setting('site_description', '');
    }
}

if (!function_exists('site_logo')) {
    /**
     * 获取站点Logo
     * 
     * @return string
     */
    function site_logo()
    {
        return system_setting('site_logo', '');
    }
}

if (!function_exists('site_keywords')) {
    /**
     * 获取站点关键词
     * 
     * @return string
     */
    function site_keywords()
    {
        return system_setting('site_keywords', '');
    }
}

if (!function_exists('is_maintenance_mode')) {
    /**
     * 检查是否处于维护模式
     * 
     * @return bool
     */
    function is_maintenance_mode()
    {
        return (bool) system_setting('maintenance_mode', false);
    }
}

if (!function_exists('get_mail_config')) {
    /**
     * 获取邮件配置
     * 
     * @return array
     */
    function get_mail_config()
    {
        return [
            'host' => system_setting('mail_host', config('mail.mailers.smtp.host')),
            'port' => system_setting('mail_port', config('mail.mailers.smtp.port')),
            'username' => system_setting('mail_username', config('mail.mailers.smtp.username')),
            'password' => system_setting('mail_password', config('mail.mailers.smtp.password')),
            'encryption' => system_setting('mail_encryption', config('mail.mailers.smtp.encryption')),
            'from_address' => system_setting('mail_from_address', config('mail.from.address')),
            'from_name' => system_setting('mail_from_name', config('mail.from.name')),
        ];
    }
}